﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Roton2.Int21Emulation
{
    /// <summary>
    /// Contains functions related to system time.
    /// </summary>
    static public class SystemTimeProvider
    {
        /// <summary>
        /// Returns system time in DOS INT 21h (AH=2Ch) format.
        /// </summary>
        static public SystemTimeResult Read()
        {
            DateTime time = DateTime.Now;
            SystemTimeResult result = new SystemTimeResult();

            result.Hundredths = (byte)(time.Millisecond / 10);
            result.Seconds = (byte)(time.Second);
            result.Minutes = (byte)(time.Minute);
            result.Hours = (byte)(time.Hour);

            return result;
        }
    }

    public struct SystemTimeResult
    {
        /// <summary>
        /// Time snapshot hours.
        /// </summary>
        public byte Hours;

        /// <summary>
        /// Time snapshot in hundredths of seconds.
        /// </summary>
        public byte Hundredths;

        /// <summary>
        /// Time snapshot minutes.
        /// </summary>
        public byte Minutes;

        /// <summary>
        /// Get the time as returned in 8086 register CH.
        /// </summary>
        public byte RegisterCH { get { return Hours; } }

        /// <summary>
        /// Get the time as returned in 8086 register CL.
        /// </summary>
        public byte RegisterCL { get { return Minutes; } }

        /// <summary>
        /// Get the time as returned in 8086 register CX.
        /// </summary>
        public int RegisterCX { get { return ((int)Hours << 8) | Minutes; } }

        /// <summary>
        /// Get the time as returned in 8086 register DH.
        /// </summary>
        public byte RegisterDH { get { return Seconds; } }

        /// <summary>
        /// Get the time as returned in 8086 register DL.
        /// </summary>
        public byte RegisterDL { get { return Hundredths; } }

        /// <summary>
        /// Get the time as returned in 8086 register DX.
        /// </summary>
        public int RegisterDX { get { return ((int)Seconds << 8) | Hundredths; } }

        /// <summary>
        /// Time snapshot seconds.
        /// </summary>
        public byte Seconds;
    }
}
